package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.RoomTimeEntity;
import com.tfjybj.itoo.teach.model.RoomTimeModel;
import com.tfjybj.itoo.teach.provider.service.RoomTimeService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * RoomTimeController
 * roomTime表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"roomTime表接口"})
@RequestMapping(value = "/roomTime")
@RestController
public class RoomTimeController {

    @Resource
    private RoomTimeService roomTimeService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model RoomTimeModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody RoomTimeModel model) {
		if (StringUtils.isEmpty(model.getRoomId())){
            return ItooResult.build(ItooResult.FAIL, "roomId为空");
        }
		if (StringUtils.isEmpty(model.getCellTimeId())){
            return ItooResult.build(ItooResult.FAIL, "cellTimeId为空");
        }
		if (StringUtils.isEmpty(model.getBuildingId())){
            return ItooResult.build(ItooResult.FAIL, "buildingId为空");
        }
        RoomTimeEntity roomTimeEntity = new RoomTimeEntity();
        BeanUtils.copyProperties(model, roomTimeEntity);
        roomTimeService.save(roomTimeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        roomTimeService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        roomTimeService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model RoomTimeModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改roomTime")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody RoomTimeModel model) {
		if (StringUtils.isEmpty(model.getRoomId())){
            return ItooResult.build(ItooResult.FAIL, "roomId为空");
        }
		if (StringUtils.isEmpty(model.getCellTimeId())){
            return ItooResult.build(ItooResult.FAIL, "cellTimeId为空");
        }
		if (StringUtils.isEmpty(model.getBuildingId())){
            return ItooResult.build(ItooResult.FAIL, "buildingId为空");
        }
        RoomTimeEntity roomTimeEntity = new RoomTimeEntity();
        BeanUtils.copyProperties(model, roomTimeEntity);
        roomTimeService.updateById(roomTimeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找RoomTime
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        RoomTimeEntity roomTimeEntity = roomTimeService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomTimeEntity);
    }

    /**
     * 分页查询所有RoomTime
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有RoomTime")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<RoomTimeEntity> roomTimes = roomTimeService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomTimes);
    }
	
	/**
     * 根据房间id模糊查询roomTime
     *
     * @param roomId  房间id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的roomTime
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据房间id模糊查询roomTime", notes = "分页根据房间id模糊查询roomTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryRoomTimeByLikeRoomId/{pageNo}/{pageSize}")
    public ItooResult queryRoomTimeByLikeRoomId(@RequestParam(required = false, defaultValue = "") String roomId,
                                                @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<RoomTimeEntity> roomTimeList = roomTimeService.queryByLikeRoomId(roomId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomTimeList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据教室id查询该教室星期节次占用情况
     * @param roomId
     * @return 已经占用的时间点(星期和节次列表)
     * @author 杨晓慧
     * @since 2.0.0 2018-11-09 19:34:10
     */
    @ApiOperation(value = "教室冲突检测",notes="根据教室id查询该教室星期节次占用情况_notes")
    @ApiImplicitParam(name="roomId",value="教室id",dataType = "string",required = true,example = "01_101")
    @GetMapping(value = "queryRoomUseByRoomId")
    public ItooResult queryRoomUseByRoomId(@RequestParam String roomId){
        List<RoomTimeModel> roomTimeModels=roomTimeService.queryRoomUseByRoomId(roomId);
        return ItooResult.build(ItooResult.SUCCESS,"检测教室冲突情况成功！",roomTimeModels);
    }


    /**
     * 根据教师id 和当前星期节次信息判定教师在当前时间段内有没有其他课：
     * @param teacherId  教师id
     * @param week  周次（星期XX）
     * @param cellTimeId 节次 （第几节课）
     * @return 返回老师在此时间点上了几节课
     * @author 杨晓慧
     * @since 2.0.0 2018-11-09 19:34:10
     */
    @ApiOperation(value = "教师冲突检测",notes="根据教师id和当前星期节次信息判定教师在当前时间段内有没有其他课")
    @ApiImplicitParams({
            @ApiImplicitParam(name="teacherId",value="教师id",dataType = "string",required = true,example = "001"),
            @ApiImplicitParam(name="week",value="周次",dataType = "string",required = true,example = "01"),
            @ApiImplicitParam(name="cellTimeId",value="节次",dataType = "string",required = true,example = "0102")
    })
    @GetMapping(value = "queryTeacherUseByTime")
    public ItooResult queryTeacherUseByTime (@RequestParam String teacherId, @RequestParam String week, @RequestParam String cellTimeId){

      int teacherUse= roomTimeService.queryTeacherUseByTime(teacherId, week, cellTimeId);

      if (teacherUse>0){
          return ItooResult.build(ItooResult.SUCCESS, "该教师在当前周次节次存在冲突！", teacherUse);
      }
        return ItooResult.build(ItooResult.SUCCESS, "该教师在当前周次节次暂未安排其他课程！", teacherUse);
    }





}    
